#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        fprintf(stderr, "Usage: %s <source> <dest>\n", argv[0]);
        return 1;
    }
    int src = open(argv[1], O_RDONLY);
    if (src < 0)
    {
        perror("Error opening file");
        return 1;
    }
    int dst=open(argv[2],O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    if (dst < 0)
    {
        perror("Error creating file");
        close(src);
        return 1;
    }
    char buffer[1024];
    ssize_t bytesRead;
    while ((bytesRead = read(src, buffer, sizeof(buffer))) > 0) {
        if (write(dst, buffer, bytesRead) < 0) {
            perror("Error writing to destination file");
            close(src);
            close(dst);
            return 1;
        }
    }
    close(src);
    close(dst);
    return 0;
}
